
const creator = '西蓝花'
// axios.get('http://hmajax.itheima.net/api/settings?creator='+creator)
axios.get('http://hmajax.itheima.net/api/settings', {
  params: {
    creator
  }
}).then(res => {
  console.log(res, 1);
  // Object.keys(res.data.data)//[avatar,desc,email,gender,nickname]
  const userInfo = res.data.data
  Object.keys(userInfo).forEach(key => {
    if (key == 'avatar') {
      /* 图片 */
      document.querySelector(`.${key}`).src = userInfo[key]

    } else if (key == 'gender') {
      /* 单选框 */
      const radioArr = document.querySelectorAll(`.${key}`)
      // 获取性别值
      const gender = userInfo[key] //1
      console.log(radioArr[gender]);
      /* 设置数组中的第几项设置状态值 */
      radioArr[gender].checked = true
    } else {
      //表单
      document.querySelector(`.${key}`).value = userInfo[key]
    }
  })
})


/* 提示框 */
const toastModal = document.querySelector('.my-toast')
const toast = new bootstrap.Toast(toastModal)
/* 修改个人信息 */
document.querySelector('.submit').addEventListener('click', () => {
  const form = document.querySelector('.user-form')
  const data = serialize(form, { hash: true, empty: true })
  console.log(data, 7);
  axios.put('http://hmajax.itheima.net/api/settings', {
    ...data,
    creator,
    gender: +data['gender']//覆盖data中的字符串类型
  }).then(res => {
    console.log(res, 89);
    if (res.statusText == "OK") {
      toast.show()
    }
  })
})